package com.yanq.liancar.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxBarTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.SmallBClassificationActivity;
import com.yanq.liancar.activity.SmallBGoodsDetailsActivity;
import com.yanq.liancar.activity.SmallBGoodsListActivity;
import com.yanq.liancar.activity.SmallBShopOrderListActivity;
import com.yanq.liancar.adapter.BMarketAdapter;
import com.yanq.liancar.base.BaseLazyFragment;
import com.yanq.liancar.bean.BShopGoodsListBean;
import com.yanq.liancar.bean.MyBanner;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.GlideImageLoader;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class BMarketFragment extends BaseLazyFragment {
    @BindView(R.id.top)
    TextView toobar;
    private Banner mBanner;
    @BindView(R.id.recycler_b)
    RecyclerView mRecyclerView;

    private View headView;
    private List<MyBanner> mBanners;
    private BMarketAdapter mMarketAdapter;
    private List<BShopGoodsListBean.DataBean.ListBean> goodsListBeans=new ArrayList<>();

    public static BMarketFragment newInstance(String param1) {
        BMarketFragment fragment = new BMarketFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int setLayoutId() {
        return R.layout.fragment_bmarket;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initOkgo();
        LogUtils.e("111111111111","BMarketFragment======onViewCreated");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.e("111111111111","BMarketFragment======onResume");
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(mActivity).statusBarDarkFont(true).init();

    }
    @Override
    protected void initView() {
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mActivity, 2));
        mMarketAdapter = new BMarketAdapter(R.layout.item_market,goodsListBeans);
        mRecyclerView.setAdapter(mMarketAdapter);

        addHeadView();

        LogUtils.e("111111111111","BMarketFragment======initView");
    }

    private void addHeadView() {
        headView = LayoutInflater.from(getActivity()).inflate(R.layout.header_bmarket, (ViewGroup) mRecyclerView.getParent(), false);
        mBanner = (Banner) headView.findViewById(R.id.banner);
        mMarketAdapter.addHeaderView(headView);
        initBannerData();
    }

    @Override
    protected void initData() {
        LogUtils.e("111111111111","BMarketFragment======initData");
    }

    @Override
    protected void setListener() {
        LogUtils.e("111111111111","BMarketFragment======setListener");

        mMarketAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putInt("goodsID",goodsListBeans.get(position).getGoodsid());
                RxActivityTool.skipActivity(mActivity, SmallBGoodsDetailsActivity.class,bundle);
            }
        });
        //商城分类
        headView.findViewById(R.id.tv_classification).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxActivityTool.skipActivity(mActivity, SmallBClassificationActivity.class);
            }
        });
        //商城订单
        headView.findViewById(R.id.tv_order).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("where", "BMarketFragment");
                RxActivityTool.skipActivity(mActivity, SmallBShopOrderListActivity.class, bundle);
            }
        });

        //四个分类
        headView.findViewById(R.id.rl_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                bundle.putString("title","正品汽车配件");
                RxActivityTool.skipActivity(mActivity, SmallBGoodsListActivity.class, bundle);
            }
        });
        headView.findViewById(R.id.rl_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 2);
                bundle.putString("title","名品折扣");
                RxActivityTool.skipActivity(mActivity, SmallBGoodsListActivity.class, bundle);
            }
        });
        headView.findViewById(R.id.rl_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("type",3);
                bundle.putString("title","热卖包邮");
                RxActivityTool.skipActivity(mActivity, SmallBGoodsListActivity.class, bundle);
            }
        });
        headView.findViewById(R.id.rl_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 4);
                bundle.putString("title","必买清单");
                RxActivityTool.skipActivity(mActivity, SmallBGoodsListActivity.class, bundle);
            }
        });

        //更多商品
        headView.findViewById(R.id.tv_more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("title","推荐商品");
                bundle.putInt("type", 0);
                RxActivityTool.skipActivity(mActivity, SmallBGoodsListActivity.class, bundle);
            }
        });

    }

    private void initBannerData() {
        mBanners = new ArrayList<>();
        MyBanner ban0 = new MyBanner(0, R.mipmap.car_bg);
        MyBanner ban1 = new MyBanner(1, R.mipmap.car_bg);
        MyBanner ban2 = new MyBanner(2, R.mipmap.car_bg);

        mBanners.add(ban0);
        mBanners.add(ban1);
        mBanners.add(ban2);
        List<Integer> imageUrl = new ArrayList<>();
        for (MyBanner myBanner : mBanners) {
            imageUrl.add(myBanner.resId);
        }
        mBanner.setImageLoader(new GlideImageLoader())
                .setImages(imageUrl)//设置图片集合
                .setDelayTime(3000)//设置轮播时间
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)//设置指示器位置（当banner模式中有指示器时）
                .setIndicatorGravity(BannerConfig.CENTER)
                .start();//banner设置方法全部调用完毕时最后调用

        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //ToastUtils.showToast(getContext(), "您点击了第" + (mBanners.get(position).type + 1) + "张照片");
            }
        });
    }

    private void initOkgo() {
        OkGo.<String>post(Config.s + "llc/api/selectClassifyGoods")
                .tag(this)
                .params("goods_classify",0)
                .params("pageNum",1)
                .params("pageSize",10)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BShopGoodsListBean selectClassifyBean = gson.fromJson(response.body(), BShopGoodsListBean.class);
                        if (selectClassifyBean.isIsSuccess()) {
                            goodsListBeans.clear();
                            goodsListBeans.addAll(selectClassifyBean.getData().getList());
                            mMarketAdapter.setNewData(goodsListBeans);
                        }else {
                            RxToast.error(selectClassifyBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }
}
